# 实现ATM存取功能的一个系统
# import json
# import os
# user = {}
# def write_file(user):
#         with open("text.txt", "w") as f:
#             f.write(json.dumps(user))
# # def check_file_exists(filename):
# #     if os.path.exists(filename):
# #         return True
# #     else:
# #         return False
# def read_file():
#     global user
#     if os.path.exists("text.txt"):
#         with open("text.txt", "r") as f:
#             user = json.loads(f.read())
#         return user
#     else:
#         write_file(user)
#
#
# def login(username, password):
#     if username in user and user[username]["password"] == password:
#         return True
#     else:
#         return False
#
# def register(username, password, balance):
#     if username in user:
#         print("该用户名已经被注册")
#     else:
#         user[username] = {"password": password, "balance": balance}
#         print("注册成功")
#         write_file(user)
#
# def deposit(amount):
#     user[username]["balance"] += amount
#     print("存款成功，当前余额为:", user[username]["balance"])
#     write_file(user)
#
# def withdraw(amount):
#     try:
#         if user[username]["balance"] >= amount:
#             user[username]["balance"] -= amount
#             print("取款成功，当前余额为:", user[username]["balance"])
#             write_file(user)
#     except:
#         print("余额不足")
# def transfer(from_user, to_user, amount):
#     try:
#         if from_user in user and to_user in user:
#             if user[from_user]["balance"] >= amount:
#                 user[from_user]["balance"] -= amount
#                 user[to_user]["balance"] += amount
#                 print("转账成功")
#                 print(f"{from_user}还剩{user[username]['balance']}")
#                 print(f"{to_user}还剩{user[to_user]['balance']}")
#                 write_file(user)
#             else:
#                 print("余额不足")
#     except:
#         print("用户不存在")
# while True:
#     read_file()
#     print("1、登录")
#     print("2、注册")
#     print("3、退出")
#     choice = input("请选择操作：")
#     if choice == "1":
#         username = input("请输入用户名：")
#         password = input("请输入密码：")
#         if login(username, password):
#             while True:
#                 print("a、存钱")
#                 print("b、取钱")
#                 print("c、转账")
#                 print("d、退出")
#                 option = input("请选择操作：")
#                 if option == "a":
#                     amount = float(input("请输入存款金额："))
#                     deposit(amount)
#                     # print(user)
#                 elif option == "b":
#                     amount = float(input("请输入取款金额："))
#                     withdraw(amount)
#                 elif option == "c":
#                     to_user = input("请输入转账对象用户名：")
#                     amount = float(input("请输入转账金额："))
#                     transfer(username, to_user, amount)
#                 elif option == "d":
#                     break
#     elif choice == "2":
#         username = input("请输入用户名：")
#         password = input("请输入密码：")
#         balance = float(input("请输入初始金额："))
#         register(username, password, balance)
#         print(user)
#     elif choice == "3":
#         break
#     else:
#         print("无效操作")


# #7、定义一个字典，value都为整数类型。输出字典中value的最大值以及它的key，输出字典中value的平均值
# d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
# d_max = float('-inf')
# d_max_key = None
# for k, v in d.items():
#     if v > d_max:
#         d_max = v
#         d_max_key = k
# print(d_max, d_max_key)
# total_num = 0
# aver_num = 0
# for v in d.values():
#     total_num = total_num + v
# aver_num = total_num / len(d)
# print(total_num)
# print(aver_num)
# print(type(d.items()), type(d.values()))


# #8、请给出随机数，范围分别在（0-1） ，(1-10), (1000-2000)
# import random
# print(random.randint(0, 1))
# print(random.randint(1, 10))
# print(random.randint(1000, 2000))


# #9、输出八天前的日期时间，格式为 "2023-12-07 12:00:00"
# import time
# now_time = time.time()
# time_ago = now_time - (8*24*60*60)
# # print(time.localtime(time_ago))
# eight_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time_ago))
# print(eight_time)


# # 10、编写python脚本，判断文件是否存在，如果存在写入内容“hello world”。判断文件路径从命令行输入。
# #    例如：python  check_file.py  -f /etc/hostname
# # #    check_file.py 为自定义模块  -f 为指定路径参数（可以随便指定）
# import argparse
# import os
# parser = argparse.ArgumentParser(description="传入一个文件名路径")
# parser.add_argument("-f","--file",help="文件名路径")
#
# args = parser.parse_args()
# file_name = args.file
#
#
# print(file_name)
# if os.path.exists(file_name):
#     with open(file_name,"a",encoding="utf-8") as f:
#         f.write("hello world")



# # 11、使用python读取/etc/ssh/sshd_config文件，读取它的前5行
# #    过滤掉以#号开头的行和空行输出
# Path_file = "/etc/ssh/sshd_config"
# lst = []
# with open(Path_file, "r") as f:
#     lines = f.readlines()
#     for line in lines:
#         if line.strip() and line.startswith("#"):
#             lst.append(line)
#     five_line = lst[:5]
# for i in five_line:
#     print(i)

#14、使用python实现5行5列的盘龙数组
# n = 5
# direction = 0
# num = 1
# lst = [[0] * n for _ in range(5)]
# top, bottom, left, right = 0, n - 1, 0, n - 1
# while top <= bottom and left <= right:
#     if direction == 0:  # 向右填充
#         for i in range(left, right+1):
#             lst[top][i] = num
#             num = num+1
#         top = top+1
#     elif direction == 1:  # 向下填充
#         for i in range(top, bottom+1):
#             lst[i][right] = num
#             num = num+1
#         right -= 1
#     elif direction == 2:  # 向左填充
#         for i in range(right, left - 1, -1):
#             lst[bottom][i] = num
#             num += 1
#         bottom -= 1
#     else:
#         for i in range(bottom, top - 1, -1):
#             lst[i][left] = num
#             num += 1
#         left += 1
#     direction = (direction + 1) % 4  # 更新方向
# print(lst)

# 有一个n阶台阶，一个人可以一次走一步或者走两步，问要想走上去有多少种方式？
# def climb_stairs_dp(n):
#     if n == 1:
#         return 1
#     elif n == 2:
#         return 2
#
#     dp = [0] * (n + 1)
#     dp[1] = 1
#     dp[2] = 2
#     for i in range(3, n + 1):
#         dp[i] = dp[i - 1] + dp[i - 2]
#
#     return dp[n]
# print(climb_stairs_dp(20))